
#include "Common/TableDataEx/EquipCfg.h"
#include "Common/TableData/EquipRefineCfg.h"
#include "Common/TableData/EquipStrengthenCfg.h"

EquipCfg::EquipCfg()
{
	m_mapRefineCfg.clear();
}

EquipCfg::~EquipCfg()
{

}

bool EquipCfg::Init()
{
	if (!Process())
	{
		return false;
	}
	return true;
}

void EquipCfg::UnInit()
{
	m_mapRefineCfg.clear();
}

const EquipRefineCfgInfo* EquipCfg::GetRefineCfgInfo(int8_t pos, int32_t refineLev, int32_t wearLev)
{
	RefineCfgMap::iterator iter = m_mapRefineCfg.find(pos);
	if (iter != m_mapRefineCfg.end())
	{
		LevRefineCfgMap &levRefineMap = iter->second;
		LevRefineCfgMap::iterator iterLev = levRefineMap.find(refineLev);
		if (iterLev == levRefineMap.end())	return nullptr;
		WearLevRefineCfgMap &wearLevMap = iterLev->second;
		WearLevRefineCfgMap::iterator wearIter = wearLevMap.find(wearLev);
		return (wearIter != wearLevMap.end()) ? wearIter->second : nullptr;
	}
	return nullptr;
}

const EquipStrengthenCfgData* EquipCfg::GetStrenthCfgInfo(int8_t prof, int32_t pos)
{
	StrenthCfgMap::iterator iter = m_mapStrenthCfg.find(prof);
	if (iter == m_mapStrenthCfg.end())
	{
		return nullptr;
	}
	PosStrenthCfgMap &posMap = iter->second;
	PosStrenthCfgMap::iterator it = posMap.find(pos);
	return (it != posMap.end()) ? &it->second : nullptr;
}

void EquipCfg::SetStrenthCostInfo(const EquipStrengthenCfgInfo* strenthCfg, EquipStrengthenCfgData &data)
{
	int32_t item_pos = strenthCfg->itemId.find('|');
	int32_t gold_pos = strenthCfg->gold.find('|');
	if (item_pos <= 0 || gold_pos <= 0)
	{
		LogErrFmtPrint("[common] EquipCfg::GetStrenthCostInfo...error...strenthCfg->itemId:%s", strenthCfg->itemId.c_str());
		return;
	}
	VEC_INT32 item_vec_param;
	VEC_FLOAT gold_vec_param;
	std::string itemStr;
	std::string goldStr;

	itemStr = strenthCfg->itemId.substr(0, item_pos);
	CommonApi::SplitStrToVecInt(itemStr, ",", &item_vec_param);
	goldStr = strenthCfg->gold.substr(0, gold_pos);
	CommonApi::SplitStrToVecFloat(goldStr, ",", &gold_vec_param);
	if (item_vec_param.size() != 6 && gold_vec_param.size() != 5)
	{
		LogErrFmtPrint("[common] EquipCfg::SetEquipStrenCost...error...strenthCfg->itemId:%s", strenthCfg->itemId.c_str());
		return;
	}
	SetEquipStrenCost(data.strenthCost_ver1, item_vec_param, gold_vec_param);
	
	itemStr = strenthCfg->itemId.substr(item_pos + 1, strenthCfg->itemId.length() - item_pos - 1);
	CommonApi::SplitStrToVecInt(itemStr, ",", &item_vec_param);
	goldStr = strenthCfg->gold.substr(gold_pos + 1, strenthCfg->gold.length() - gold_pos - 1);
	CommonApi::SplitStrToVecFloat(goldStr, ",", &gold_vec_param);
	if (item_vec_param.size() != 6 && gold_vec_param.size() != 5)
	{
		LogErrFmtPrint("[common] EquipCfg::SetEquipStrenCost...error...strenthCfg->itemId:%s", strenthCfg->itemId.c_str());
		return;
	}
	SetEquipStrenCost(data.strenthCost_ver2, item_vec_param, gold_vec_param);
}

void EquipCfg::SetEquipStrenCost(EquipStrenCost &cost, const VEC_INT32 &itemVec, VEC_FLOAT &goldVec)
{
	cost.itemId = itemVec[2];
	cost.item_param_a = itemVec[3];
	cost.item_param_b = itemVec[4];
	cost.item_param_c = itemVec[5];
	cost.gold_param_a = goldVec[0];
	cost.gold_param_b = goldVec[1];
	cost.gold_param_c = goldVec[2];
	cost.gold_param_d = goldVec[3];
	cost.gold_param_e = goldVec[4];
}

bool EquipCfg::Process()
{
	const EquipRefineCfgMap *pRefineCfgMap = g_GetEquipRefineCfgTable()->GetEquipRefineCfgMap();
	if (nullptr != pRefineCfgMap)
	{
		EquipRefineCfgMap::const_iterator iter = pRefineCfgMap->begin();
		for (; iter != pRefineCfgMap->end(); ++iter)
		{
			const EquipRefineCfgInfo &refineCfg = iter->second;

			RefineCfgMap::iterator iterRefine = m_mapRefineCfg.find(refineCfg.position);
			if (iterRefine != m_mapRefineCfg.end())
			{
				LevRefineCfgMap &levRefineMap = iterRefine->second;
				levRefineMap[refineCfg.refineLv][refineCfg.wearLevel] = &refineCfg;
			}
			else
			{
				m_mapRefineCfg[refineCfg.position][refineCfg.refineLv][refineCfg.wearLevel] = &refineCfg;
			}
		}
	}

	const EquipStrengthenCfgMap* pStrenthCfgMap = g_GetEquipStrengthenCfgTable()->GetEquipStrengthenCfgMap();
	if (nullptr != pStrenthCfgMap)
	{
		EquipStrengthenCfgMap::const_iterator iter = pStrenthCfgMap->begin();
		for (; iter != pStrenthCfgMap->end(); ++iter)
		{
			const EquipStrengthenCfgInfo &strenthCfg = iter->second;
			EquipStrengthenCfgData strenthData;
			strenthData.strenthCfg = &strenthCfg;
			SetStrenthCostInfo(&strenthCfg, strenthData);
			StrenthCfgMap::iterator it = m_mapStrenthCfg.find(strenthCfg.profession);
			if (it != m_mapStrenthCfg.end())
			{
				PosStrenthCfgMap &posMap = it->second;
				posMap[strenthCfg.position] = strenthData;
			}
			else
			{
				PosStrenthCfgMap posMap;
				posMap[strenthCfg.position] = strenthData;
				m_mapStrenthCfg[strenthCfg.profession] = posMap;
			}
		}
	}
	return true;
}